package com.hsytech.dijiebao.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

public class SystemManager extends Application {
    private static boolean isExit = false;

    private static List<Activity> mList = new LinkedList<Activity>();
    // Ϊ��ʵ��ÿ��ʹ�ø���ʱ�������µĶ���������ľ�̬����
    private static SystemManager instance;

    // ���췽��
    private SystemManager() {

    }

    // ʵ����һ��
    public synchronized static SystemManager getInstance() {
        if (null == instance) {
            instance = new SystemManager();
        }
        return instance;
    }

    // add Activity
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    // �ر�ÿһ��list�ڵ�activity
    public static void exit() {
        try {
            for (int i = mList.size() - 1; i >= 0; i--) {
                Activity activity = mList.get(i);
                // Log.e("----", i + "");
                if (activity != null) {
                    activity.finish();
                    mList.remove(activity);
                    activity = null;

                } else {
                    android.os.Process.killProcess(android.os.Process.myPid());
                    System.exit(0);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // ĳЩactivity����Ҫ��¼
    public void removeActivity(Activity activity) {
        if (mList != null) {
            mList.remove(activity);
        }
    }

    public Activity getCurrActivity() {
        return mList.get(mList.size() - 1);
    }

    public void finishThisActivity(Activity activity) {
        for (int i = mList.size() - 1; i >= 0; i--) {
            Activity a = mList.get(i);

            if (null == a) {
                mList.remove(a);
            }

            if (null != a && activity.getClass().toString().equals(a.getClass().toString())) {
                mList.remove(a);

                a.finish();
                a = null;
            }
        }
    }

    /**
     * @author mo
     */
    public static void finishCurrActivity(Activity activity) {

        for (int i = mList.size() - 1; i >= 0; i--) {
            Activity a = mList.get(i);

            if (null == a) {
                mList.remove(a);
            }

            String name = activity.getClass().getSimpleName();
            Log.e("---quit name", name);
            // quitHere��Զ��Ϊ��
            String quitHere = SystemSettings.ExitActivity.exitName;
            if (a != null && a.getClass().getSimpleName().equals(name)) {

                if (name.equals(quitHere)) {
                    // Toast.makeText(a, "this is main", 0).show();
                    doExit(activity);
                    return;
                } else {
                    // Toast.makeText(a, "this is not main", 0).show();
                    mList.remove(a);
                    a.finish();
                    a = null;
                    return;
                }
            }
        }
    }

    // ɱ����
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }

    private static Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }

        ;
    };

    private static void doExit(Activity activity) {

        if (isExit == false) {

            isExit = true;
            Toast.makeText(activity, "再按一次离开", Toast.LENGTH_SHORT).show();
            handler.sendEmptyMessageDelayed(0, 2000);

        } else {

            activity.finish();
            activity = null;
            exit();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);

        }

    }

    public boolean isBackgroundRunning(Context context) {
        String processName = SystemSettings.RunningInBackground.progressName;

        ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
        if (activityManager == null)
            return false;
        // get running application processes
        List<ActivityManager.RunningAppProcessInfo> processList = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo process : processList) {
            if (process.processName.startsWith(processName)) {
                boolean isBackground = process.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                        && process.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
                boolean isLockedState = keyguardManager.inKeyguardRestrictedInputMode();
                if (isBackground || isLockedState)
                    return true;
                else
                    return false;
            }
        }
        return false;
    }

}
